#include "bsp_uart.h"
#include "bsp_gpio.h"

//串口屏 接收数据缓存区
uint8_t ScreenUart_RxBuffer[100];

UART_HandleTypeDef DEBUG_UART_Handler;
UART_HandleTypeDef SCREEN_UART_Handler;


/* DEBUG 串口 初始化 */
void DebugUart_Init(void)
{
    DebugUart_GPIO_Init();
    DEBUG_UART_CLK_ENABLE();
    DEBUG_UART_Handler.Instance = DEBUG_UARTx;
    DEBUG_UART_Handler.Init.BaudRate = 115200;
    DEBUG_UART_Handler.Init.WordLength = UART_WORDLENGTH_8B;
    DEBUG_UART_Handler.Init.StopBits = UART_STOPBITS_1;
    DEBUG_UART_Handler.Init.Parity = UART_PARITY_NONE;
    DEBUG_UART_Handler.Init.HwFlowCtl = UART_HWCONTROL_NONE;
    DEBUG_UART_Handler.Init.Mode = UART_MODE_TX_RX;
    HAL_UART_Init(&DEBUG_UART_Handler);
}
/* DEBUG 串口 发送数据函数 */
void DebugUart_SendData(uint8_t *data)
{   
    int len = 0;
    do{
        HAL_UART_Transmit(&DEBUG_UART_Handler, (data + len), 1, 1000);
        len++;
    }while(*(data + len) != '\0');
}

/* LCD 串口初始化 */
void ScreenUart_Init(void)
{
    ScreenUart_GPIO_Init();
    SCREEN_UART_CLK_ENABLE();
    SCREEN_UART_Handler.Instance = SCREEN_UARTx;
    SCREEN_UART_Handler.Init.BaudRate = 9600;
    SCREEN_UART_Handler.Init.WordLength = UART_WORDLENGTH_8B;
    SCREEN_UART_Handler.Init.StopBits = UART_STOPBITS_1;
    SCREEN_UART_Handler.Init.Parity = UART_PARITY_NONE;
    SCREEN_UART_Handler.Init.HwFlowCtl = UART_HWCONTROL_NONE;
    SCREEN_UART_Handler.Init.Mode = UART_MODE_TX_RX;
    HAL_UART_Init(&SCREEN_UART_Handler);
    // 使能 串口接收空闲中断
    __HAL_UART_ENABLE_IT(&SCREEN_UART_Handler, UART_IT_IDLE);
    HAL_NVIC_SetPriority(SCREEN_UART_IRQn, 5, 0);
    HAL_NVIC_EnableIRQ(SCREEN_UART_IRQn);
    //启动串口空闲中断接收
    HAL_UARTEx_ReceiveToIdle_IT(&SCREEN_UART_Handler, (uint8_t *)ScreenUart_RxBuffer, 100);
}
/* LCD 串口发送函数 */
void ScreenUart_SendData(uint8_t *data)
{
    int len = 0;
    do{
        HAL_UART_Transmit(&SCREEN_UART_Handler, (data + len), 1, 1000);
        len++;
    }while(*(data + len)!= '\0');
}

/*  */
